package com.simple.portal.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.portal.dao.IApplicationDAO;
import com.simple.portal.vo.ApplicationVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;
@Repository("applicationDAO")
public class ApplicationDAOImpl extends BaseDAO implements IApplicationDAO {

	@Override
	public ApplicationVO findById(final String id) {
		return  super.get(ApplicationVO.class,id);
	}

	@Override
	public void save(final ApplicationVO applicationVO,final String operator) {
		ApplicationVO tempApplicationVO = super.save(applicationVO);
		super.saveLogger(operator, tempApplicationVO, tempApplicationVO.getApplicationId(),Constants.LOGGER_OPERATE_FLAG_ADD);

	}
	
	@Override
	public void update(final ApplicationVO applicationVO,final String type,final String operator) {
		super.update(applicationVO);
		super.saveLogger(operator, applicationVO, applicationVO.getApplicationId(),type);

	}

	@Override
	public List<ApplicationVO> findAll() {
		return super.query(ApplicationVO.class);
	}

	@Override
	public void delete(final ApplicationVO applicationVO,final String operator) {
		super.delete(applicationVO);
		super.saveLogger(operator, applicationVO, applicationVO.getApplicationId(), Constants.LOGGER_OPERATE_FLAG_DELETE);

	}

	@Override
	public void delete(final String id,final String operator) {
		ApplicationVO applicationVO = findById(id);
		super.delete(ApplicationVO.class,id);
		super.saveLogger(operator, applicationVO, applicationVO.getApplicationId(), Constants.LOGGER_OPERATE_FLAG_DELETE);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<?> findApplicationByPage(String userId, String appName,
			String providerName, PageInfoBean pageInfoBean) {
		final StringBuffer sb = new StringBuffer(300);
	 	final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
	 	
        sb.append( "select a,org from ApplicationVO a,OrganizationVO org  where a.supplier = org.organizationId ");
        sb.append(" and a.creator =:userId ");
	 	paramMap.put("userId", userId);
	 	
	 	if(!StringUtil.isNull(appName)){
            sb.append( " and a.applicationName like :applicationName" );
            paramMap.put("applicationName", "%"+appName+"%");
        }
	 	if(!StringUtil.isNull(providerName)){
            sb.append( " and org.organizationId=:providerName" );
            paramMap.put("providerName", providerName);
        }
	 	sb.append( " order by a.createDate desc" );

		return  (List<ApplicationVO>)super.queryListByPage("a.applicationId", sb.toString(), paramMap, pageInfoBean);
	}

	
	@Override
	public List<?> findApplicationByPageByOrganization(final String organizationId, final String applicationTypeKeyId, final PageInfoBean pageInfoBean){
		final StringBuffer sb = new StringBuffer(300);
		final Map<String, Object> paramMap = new LinkedHashMap<String, Object>();

		sb.append("select a,org from ApplicationVO a,OrganizationVO org,ApplicationApplyVO ap  where a.supplier = org.organizationId and a.applicationId = ap.applicationId ");
		sb.append(" and  ap.applyStatus=:applyStatus");
		
		paramMap.put("applyStatus", Constants.STATE_NORMAL);
		
		if (!StringUtil.isNull(organizationId)) {
			sb.append(" and a.organizationId =:organizationId ");
			paramMap.put("organizationId", organizationId);
		}
		if (!StringUtil.isNull(applicationTypeKeyId)) {
			sb.append(" and a.applicationTypeKeyId=:applicationTypeKeyId ");
			paramMap.put("applicationTypeKeyId", applicationTypeKeyId);
		}
		sb.append( " order by a.createDate desc" );

		return super.queryListByPage("a.applicationId", sb.toString(), paramMap, pageInfoBean);
		
	}
}
